package com.yunshan.mp;

import com.yunshan.common.annotation.RepeatSubmit;
import com.yunshan.common.core.controller.BaseController;
import com.yunshan.common.core.domain.AjaxResult;
import com.yunshan.common.utils.UserUtils;
import com.yunshan.wechat.domain.UserInfoVo;
import com.yunshan.wechat.service.IWechatUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import static com.yunshan.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 小程序用户信息
 *
 * @author yunshan
 * @date 2025-02-19
 */
@Api(tags = "小程序用户信息")
@RestController
@RequestMapping("/wx/user")
public class MpUserController  extends BaseController {

    @Autowired
    private IWechatUserInfoService wechatUserInfoService;

    @ApiOperation("获取用户信息")
    @GetMapping()
    private AjaxResult getWechatUserInfo()
    {
        try {
            Long userId = UserUtils.getUserId();
            if (userId == null) {
                return AjaxResult.error("用户登录异常");
            }
            UserInfoVo userInfoVo = wechatUserInfoService.selectUserInfoById(userId);
            if (userInfoVo == null) {
                return AjaxResult.error("用户信息异常");
            }

            return success(userInfoVo);
        }catch (Exception e){
            log.error("获取用户信息失败：{}",e.toString());
            return AjaxResult.error("获取用户信息失败");
        }
    }

    @ApiOperation("修改用户信息")
    @RepeatSubmit
    @PutMapping()
    private AjaxResult update(
            @ApiParam(name = "userInfoVo", value = "用户信息对象", required = true)
            @RequestBody UserInfoVo userInfoVo)
    {
        return toAjax(wechatUserInfoService.updateUniUserInfo(userInfoVo));
    }

}
